// 省市区三级联动选择
import {getCityTree} from '@/api/data.js'
const selectCity = {
	data(){
		return {
			cityList:[]
		}
	},
	created(){
		getCityTree({parentId:-1}).then(res=>{
			this.cityList = this.filterThree(res.content,4);
		})
	},
	methods:{
		filterThree(arr,lv){
			return arr.filter(val=>{
				if(val.children) {
					val.children = this.filterThree(val.children,lv);
					val.children.length===0 && delete val.children;
				}
				val.extra = {
					cityName:val.name,
					cityPath:val.mergeName,
					cityId:val.id,
					cityPinyin:val.pinyin
				}
				return val.level!=lv;
			})
		},
		returnAddress(item){
			const {province,city,area} = item;
			const selectCityArr = [province];
			if(city.id){
				selectCityArr.push(city)
			}
			if(area.id){
				selectCityArr.push(area)
			}
			const address = selectCityArr[selectCityArr.length - 1];
			return address || {}
		},
		//获取选中的城市地址
		getSelectCity(item){
			const address = this.returnAddress(item);
			const form = {
				...this.form,
				cityName:address.name,
				cityPath:address.mergeName,
				cityId:address.id,
				cityPinyin:address.pinyin
			}; //选中的数据赋值给form
			const mergeName = address.mergeName.split(',').join('-');
			console.log(address)
			this.isStoreDetail ? form.cityAddress = mergeName : form.address = mergeName;
			this.form = form
		}
	}
}
export default selectCity